{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>candle_begin_time</th>\n",
       "      <th>symbol</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-01-24 16:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>1.97e-04</td>\n",
       "      <td>1.97e-04</td>\n",
       "      <td>3.01e-05</td>\n",
       "      <td>1.35e-04</td>\n",
       "      <td>27949.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-01-24 17:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>7.90e-05</td>\n",
       "      <td>9.99e-05</td>\n",
       "      <td>4.70e-05</td>\n",
       "      <td>5.60e-05</td>\n",
       "      <td>51104.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-01-24 18:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>6.30e-05</td>\n",
       "      <td>7.47e-05</td>\n",
       "      <td>5.01e-05</td>\n",
       "      <td>7.18e-05</td>\n",
       "      <td>106084.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-01-24 19:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>7.17e-05</td>\n",
       "      <td>7.33e-05</td>\n",
       "      <td>6.11e-05</td>\n",
       "      <td>7.04e-05</td>\n",
       "      <td>60643.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-01-24 20:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>7.00e-05</td>\n",
       "      <td>7.70e-05</td>\n",
       "      <td>6.10e-05</td>\n",
       "      <td>6.10e-05</td>\n",
       "      <td>26093.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2145</th>\n",
       "      <td>2018-01-24 19:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.60e+00</td>\n",
       "      <td>1.62e+00</td>\n",
       "      <td>1.58e+00</td>\n",
       "      <td>1.62e+00</td>\n",
       "      <td>8728.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2146</th>\n",
       "      <td>2018-01-24 20:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.61e+00</td>\n",
       "      <td>1.66e+00</td>\n",
       "      <td>1.61e+00</td>\n",
       "      <td>1.66e+00</td>\n",
       "      <td>45549.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2147</th>\n",
       "      <td>2018-01-24 21:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.70e+00</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.67e+00</td>\n",
       "      <td>95792.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2148</th>\n",
       "      <td>2018-01-24 22:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.66e+00</td>\n",
       "      <td>1.68e+00</td>\n",
       "      <td>1.64e+00</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>21623.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2149</th>\n",
       "      <td>2018-01-24 23:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.68e+00</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.68e+00</td>\n",
       "      <td>1202.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2150 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        candle_begin_time  symbol      open      high       low     close     volume\n",
       "0     2018-01-24 16:00:00  AIDBTC  1.97e-04  1.97e-04  3.01e-05  1.35e-04   27949.99\n",
       "1     2018-01-24 17:00:00  AIDBTC  7.90e-05  9.99e-05  4.70e-05  5.60e-05   51104.00\n",
       "2     2018-01-24 18:00:00  AIDBTC  6.30e-05  7.47e-05  5.01e-05  7.18e-05  106084.15\n",
       "3     2018-01-24 19:00:00  AIDBTC  7.17e-05  7.33e-05  6.11e-05  7.04e-05   60643.77\n",
       "4     2018-01-24 20:00:00  AIDBTC  7.00e-05  7.70e-05  6.10e-05  6.10e-05   26093.50\n",
       "...                   ...     ...       ...       ...       ...       ...        ...\n",
       "2145  2018-01-24 19:00:00  ZRXUSD  1.60e+00  1.62e+00  1.58e+00  1.62e+00    8728.98\n",
       "2146  2018-01-24 20:00:00  ZRXUSD  1.61e+00  1.66e+00  1.61e+00  1.66e+00   45549.64\n",
       "2147  2018-01-24 21:00:00  ZRXUSD  1.65e+00  1.70e+00  1.65e+00  1.67e+00   95792.54\n",
       "2148  2018-01-24 22:00:00  ZRXUSD  1.66e+00  1.68e+00  1.64e+00  1.65e+00   21623.11\n",
       "2149  2018-01-24 23:00:00  ZRXUSD  1.65e+00  1.68e+00  1.65e+00  1.68e+00    1202.25\n",
       "\n",
       "[2150 rows x 7 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 对print出的数据格式进行修正\n",
    "pd.set_option('expand_frame_repr', False)  # 当列太多时不换行\n",
    "pd.set_option('precision', 2)  # 浮点数的精度\n",
    "pd.set_option('display.max_rows', 10) # 显示的最大行数\n",
    "\n",
    "# =====导入数据\n",
    "df = pd.read_csv(\n",
    "    r'C:\\notebooks\\quantitative_trading_notes\\data\\BITFINEX-1H-data-20180124.csv',\n",
    "    skiprows=1,\n",
    "    # nrows=15,\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        candle_begin_time  symbol      open      high       low     close    volume\n",
      "2149  2018-01-24 23:00:00  ZRXUSD  1.65e+00  1.68e+00  1.65e+00  1.68e+00   1202.25\n",
      "92    2018-01-24 23:00:00  AVTUSD  3.68e+00  3.73e+00  3.63e+00  3.73e+00   1345.79\n",
      "233   2018-01-24 23:00:00  BCHUSD  1.62e+03  1.66e+03  1.61e+03  1.65e+03    260.56\n",
      "1722  2018-01-24 23:00:00  SPKBTC  3.90e-05  4.00e-05  3.90e-05  3.96e-05  24443.20\n",
      "1698  2018-01-24 23:00:00  SNTUSD  2.81e-01  2.90e-01  2.81e-01  2.90e-01  39775.92\n",
      "...                   ...     ...       ...       ...       ...       ...       ...\n",
      "117   2018-01-24 00:00:00  BATETH  5.92e-04  6.40e-04  5.89e-04  5.89e-04  23291.86\n",
      "449   2018-01-24 00:00:00  EDOBTC  2.94e-04  3.00e-04  2.94e-04  3.00e-04   1822.53\n",
      "640   2018-01-24 00:00:00  ETCUSD  2.82e+01  2.84e+01  2.80e+01  2.80e+01  38178.85\n",
      "1092  2018-01-24 00:00:00  MNAETH  1.48e-04  1.48e-04  1.45e-04  1.46e-04  21369.91\n",
      "1020  2018-01-24 00:00:00  LTCBTC  1.64e-02  1.65e-02  1.64e-02  1.64e-02   1347.99\n",
      "\n",
      "[2150 rows x 7 columns]\n",
      "        candle_begin_time  symbol      open      high       low     close     volume\n",
      "2149  2018-01-24 23:00:00  ZRXUSD  1.65e+00  1.68e+00  1.65e+00  1.68e+00    1202.25\n",
      "2148  2018-01-24 22:00:00  ZRXUSD  1.66e+00  1.68e+00  1.64e+00  1.65e+00   21623.11\n",
      "2147  2018-01-24 21:00:00  ZRXUSD  1.65e+00  1.70e+00  1.65e+00  1.67e+00   95792.54\n",
      "2146  2018-01-24 20:00:00  ZRXUSD  1.61e+00  1.66e+00  1.61e+00  1.66e+00   45549.64\n",
      "2145  2018-01-24 19:00:00  ZRXUSD  1.60e+00  1.62e+00  1.58e+00  1.62e+00    8728.98\n",
      "...                   ...     ...       ...       ...       ...       ...        ...\n",
      "4     2018-01-24 20:00:00  AIDBTC  7.00e-05  7.70e-05  6.10e-05  6.10e-05   26093.50\n",
      "3     2018-01-24 19:00:00  AIDBTC  7.17e-05  7.33e-05  6.11e-05  7.04e-05   60643.77\n",
      "2     2018-01-24 18:00:00  AIDBTC  6.30e-05  7.47e-05  5.01e-05  7.18e-05  106084.15\n",
      "1     2018-01-24 17:00:00  AIDBTC  7.90e-05  9.99e-05  4.70e-05  5.60e-05   51104.00\n",
      "0     2018-01-24 16:00:00  AIDBTC  1.97e-04  1.97e-04  3.01e-05  1.35e-04   27949.99\n",
      "\n",
      "[2150 rows x 7 columns]\n"
     ]
    }
   ],
   "source": [
    "print(df.sort_values(by=['candle_begin_time'], ascending=0))  # by参数指定按照什么进行排序，acsending参数指定是顺序还是逆序，1顺序，0逆序\n",
    "print(df.sort_values(by=['symbol', 'candle_begin_time'], ascending=[0, 0]))  # 按照多列进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     candle_begin_time  symbol     close     volume\n",
      "0  2018-01-24 16:00:00  AIDBTC  1.35e-04   27949.99\n",
      "1  2018-01-24 17:00:00  AIDBTC  5.60e-05   51104.00\n",
      "2  2018-01-24 18:00:00  AIDBTC  7.18e-05  106084.15\n",
      "3  2018-01-24 19:00:00  AIDBTC  7.04e-05   60643.77\n",
      "4  2018-01-24 20:00:00  AIDBTC  6.10e-05   26093.50\n",
      "5  2018-01-24 21:00:00  AIDBTC  6.79e-05    1332.15\n",
      "6  2018-01-24 22:00:00  AIDBTC  7.28e-05   31376.83\n",
      "7  2018-01-24 23:00:00  AIDBTC  6.91e-05   19032.53\n",
      "8  2018-01-24 16:00:00  AIDETH  7.27e-04   53803.15\n",
      "9  2018-01-24 17:00:00  AIDETH  6.02e-04   86513.57\n",
      "      candle_begin_time  symbol     close     volume\n",
      "5   2018-01-24 21:00:00  AIDBTC  6.79e-05    1332.15\n",
      "6   2018-01-24 22:00:00  AIDBTC  7.28e-05   31376.83\n",
      "7   2018-01-24 23:00:00  AIDBTC  6.91e-05   19032.53\n",
      "8   2018-01-24 16:00:00  AIDETH  7.27e-04   53803.15\n",
      "9   2018-01-24 17:00:00  AIDETH  6.02e-04   86513.57\n",
      "10  2018-01-24 18:00:00  AIDETH  6.10e-04  203162.96\n",
      "11  2018-01-24 19:00:00  AIDETH  7.10e-04   40905.04\n",
      "12  2018-01-24 20:00:00  AIDETH  5.11e-04  271016.75\n",
      "13  2018-01-24 21:00:00  AIDETH  7.64e-04    1687.99\n",
      "14  2018-01-24 22:00:00  AIDETH  7.62e-04     600.00\n",
      "      candle_begin_time  symbol     close     volume\n",
      "0   2018-01-24 16:00:00  AIDBTC  1.35e-04   27949.99\n",
      "1   2018-01-24 17:00:00  AIDBTC  5.60e-05   51104.00\n",
      "2   2018-01-24 18:00:00  AIDBTC  7.18e-05  106084.15\n",
      "3   2018-01-24 19:00:00  AIDBTC  7.04e-05   60643.77\n",
      "4   2018-01-24 20:00:00  AIDBTC  6.10e-05   26093.50\n",
      "..                  ...     ...       ...        ...\n",
      "10  2018-01-24 18:00:00  AIDETH  6.10e-04  203162.96\n",
      "11  2018-01-24 19:00:00  AIDETH  7.10e-04   40905.04\n",
      "12  2018-01-24 20:00:00  AIDETH  5.11e-04  271016.75\n",
      "13  2018-01-24 21:00:00  AIDETH  7.64e-04    1687.99\n",
      "14  2018-01-24 22:00:00  AIDETH  7.62e-04     600.00\n",
      "\n",
      "[20 rows x 4 columns]\n",
      "      candle_begin_time  symbol     close     volume\n",
      "0   2018-01-24 16:00:00  AIDBTC  1.35e-04   27949.99\n",
      "1   2018-01-24 17:00:00  AIDBTC  5.60e-05   51104.00\n",
      "2   2018-01-24 18:00:00  AIDBTC  7.18e-05  106084.15\n",
      "3   2018-01-24 19:00:00  AIDBTC  7.04e-05   60643.77\n",
      "4   2018-01-24 20:00:00  AIDBTC  6.10e-05   26093.50\n",
      "..                  ...     ...       ...        ...\n",
      "15  2018-01-24 18:00:00  AIDETH  6.10e-04  203162.96\n",
      "16  2018-01-24 19:00:00  AIDETH  7.10e-04   40905.04\n",
      "17  2018-01-24 20:00:00  AIDETH  5.11e-04  271016.75\n",
      "18  2018-01-24 21:00:00  AIDETH  7.64e-04    1687.99\n",
      "19  2018-01-24 22:00:00  AIDETH  7.62e-04     600.00\n",
      "\n",
      "[20 rows x 4 columns]\n"
     ]
    }
   ],
   "source": [
    "# 两个df上下合并操作，append操作\n",
    "df1 = df.iloc[0:10][['candle_begin_time', 'symbol', 'close', 'volume']]\n",
    "print(df1)\n",
    "df2 = df.iloc[5:15][['candle_begin_time', 'symbol', 'close', 'volume']]\n",
    "print(df2)\n",
    "print(df1.append(df2))  # append操作，将df1和df2上下拼接起来。注意观察拼接之后的index。index可以重复\n",
    "df3 = df1.append(df2, ignore_index=True)  # ignore_index参数，用户重新确定index\n",
    "print(df3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      candle_begin_time  symbol     close     volume\n",
      "0   2018-01-24 16:00:00  AIDBTC  1.35e-04   27949.99\n",
      "1   2018-01-24 17:00:00  AIDBTC  5.60e-05   51104.00\n",
      "2   2018-01-24 18:00:00  AIDBTC  7.18e-05  106084.15\n",
      "3   2018-01-24 19:00:00  AIDBTC  7.04e-05   60643.77\n",
      "4   2018-01-24 20:00:00  AIDBTC  6.10e-05   26093.50\n",
      "..                  ...     ...       ...        ...\n",
      "15  2018-01-24 18:00:00  AIDETH  6.10e-04  203162.96\n",
      "16  2018-01-24 19:00:00  AIDETH  7.10e-04   40905.04\n",
      "17  2018-01-24 20:00:00  AIDETH  5.11e-04  271016.75\n",
      "18  2018-01-24 21:00:00  AIDETH  7.64e-04    1687.99\n",
      "19  2018-01-24 22:00:00  AIDETH  7.62e-04     600.00\n",
      "\n",
      "[15 rows x 4 columns]\n"
     ]
    }
   ],
   "source": [
    "# 对数据进行去重\n",
    "# df3中有重复的行数，我们如何将重复的行数去除？\n",
    "df3.drop_duplicates(\n",
    "    subset=['candle_begin_time', 'symbol'],  # subset参数用来指定根据哪类类数据来判断是否重复。若不指定，则用全部列的数据来判断是否重复\n",
    "    keep='first',  # 在去除重复值的时候，我们是保留上面一行还是下面一行？first保留上面一行，last保留下面一行，False就是一行都不保留\n",
    "    inplace=True\n",
    ")\n",
    "print(df3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      index    candle_begin_time  symbol      open      high       low     close     volume\n",
      "0         0  2018-01-24 16:00:00  AIDBTC  1.97e-04  1.97e-04  3.01e-05  1.35e-04   27949.99\n",
      "1         1  2018-01-24 17:00:00  AIDBTC  7.90e-05  9.99e-05  4.70e-05  5.60e-05   51104.00\n",
      "2         2  2018-01-24 18:00:00  AIDBTC  6.30e-05  7.47e-05  5.01e-05  7.18e-05  106084.15\n",
      "3         3  2018-01-24 19:00:00  AIDBTC  7.17e-05  7.33e-05  6.11e-05  7.04e-05   60643.77\n",
      "4         4  2018-01-24 20:00:00  AIDBTC  7.00e-05  7.70e-05  6.10e-05  6.10e-05   26093.50\n",
      "...     ...                  ...     ...       ...       ...       ...       ...        ...\n",
      "2145   2145  2018-01-24 19:00:00  ZRXUSD  1.60e+00  1.62e+00  1.58e+00  1.62e+00    8728.98\n",
      "2146   2146  2018-01-24 20:00:00  ZRXUSD  1.61e+00  1.66e+00  1.61e+00  1.66e+00   45549.64\n",
      "2147   2147  2018-01-24 21:00:00  ZRXUSD  1.65e+00  1.70e+00  1.65e+00  1.67e+00   95792.54\n",
      "2148   2148  2018-01-24 22:00:00  ZRXUSD  1.66e+00  1.68e+00  1.64e+00  1.65e+00   21623.11\n",
      "2149   2149  2018-01-24 23:00:00  ZRXUSD  1.65e+00  1.68e+00  1.65e+00  1.68e+00    1202.25\n",
      "\n",
      "[2150 rows x 8 columns]\n",
      "        candle_begin_time  symbol      open      high       low     close     volume\n",
      "0     2018-01-24 16:00:00  AIDBTC  1.97e-04  1.97e-04  3.01e-05  1.35e-04   27949.99\n",
      "1     2018-01-24 17:00:00  AIDBTC  7.90e-05  9.99e-05  4.70e-05  5.60e-05   51104.00\n",
      "2     2018-01-24 18:00:00  AIDBTC  6.30e-05  7.47e-05  5.01e-05  7.18e-05  106084.15\n",
      "3     2018-01-24 19:00:00  AIDBTC  7.17e-05  7.33e-05  6.11e-05  7.04e-05   60643.77\n",
      "4     2018-01-24 20:00:00  AIDBTC  7.00e-05  7.70e-05  6.10e-05  6.10e-05   26093.50\n",
      "...                   ...     ...       ...       ...       ...       ...        ...\n",
      "2145  2018-01-24 19:00:00  ZRXUSD  1.60e+00  1.62e+00  1.58e+00  1.62e+00    8728.98\n",
      "2146  2018-01-24 20:00:00  ZRXUSD  1.61e+00  1.66e+00  1.61e+00  1.66e+00   45549.64\n",
      "2147  2018-01-24 21:00:00  ZRXUSD  1.65e+00  1.70e+00  1.65e+00  1.67e+00   95792.54\n",
      "2148  2018-01-24 22:00:00  ZRXUSD  1.66e+00  1.68e+00  1.64e+00  1.65e+00   21623.11\n",
      "2149  2018-01-24 23:00:00  ZRXUSD  1.65e+00  1.68e+00  1.65e+00  1.68e+00    1202.25\n",
      "\n",
      "[2150 rows x 7 columns]\n"
     ]
    }
   ],
   "source": [
    "# =====重新设置index\n",
    "print(df.reset_index(inplace=False, drop=False))\n",
    "print(df.reset_index(inplace=False, drop=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        candle_begin_time  symbol       开盘价      high       low       收盘价     volume\n",
      "0     2018-01-24 16:00:00  AIDBTC  1.97e-04  1.97e-04  3.01e-05  1.35e-04   27949.99\n",
      "1     2018-01-24 17:00:00  AIDBTC  7.90e-05  9.99e-05  4.70e-05  5.60e-05   51104.00\n",
      "2     2018-01-24 18:00:00  AIDBTC  6.30e-05  7.47e-05  5.01e-05  7.18e-05  106084.15\n",
      "3     2018-01-24 19:00:00  AIDBTC  7.17e-05  7.33e-05  6.11e-05  7.04e-05   60643.77\n",
      "4     2018-01-24 20:00:00  AIDBTC  7.00e-05  7.70e-05  6.10e-05  6.10e-05   26093.50\n",
      "...                   ...     ...       ...       ...       ...       ...        ...\n",
      "2145  2018-01-24 19:00:00  ZRXUSD  1.60e+00  1.62e+00  1.58e+00  1.62e+00    8728.98\n",
      "2146  2018-01-24 20:00:00  ZRXUSD  1.61e+00  1.66e+00  1.61e+00  1.66e+00   45549.64\n",
      "2147  2018-01-24 21:00:00  ZRXUSD  1.65e+00  1.70e+00  1.65e+00  1.67e+00   95792.54\n",
      "2148  2018-01-24 22:00:00  ZRXUSD  1.66e+00  1.68e+00  1.64e+00  1.65e+00   21623.11\n",
      "2149  2018-01-24 23:00:00  ZRXUSD  1.65e+00  1.68e+00  1.65e+00  1.68e+00    1202.25\n",
      "\n",
      "[2150 rows x 7 columns]\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 其他函数\n",
    "print(df.rename(columns={'close': '收盘价', 'open': '开盘价'}))  # rename函数给变量修改名字。使用dict将要修改的名字传给columns参数\n",
    "print(df.empty)  # 判断一个df是不是为空，此处输出不为空\n",
    "print(pd.DataFrame().empty)  # pd.DataFrame()创建一个空的DataFrame，此处输出为空"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                  0                    1                    2                    3                    4                    5                    6                    7                    8                    9            ...                          2140                 2141                 2142                 2143                 2144                 2145                 2146                 2147                 2148                 2149\n",
      "candle_begin_time  2018-01-24 16:00:00  2018-01-24 17:00:00  2018-01-24 18:00:00  2018-01-24 19:00:00  2018-01-24 20:00:00  2018-01-24 21:00:00  2018-01-24 22:00:00  2018-01-24 23:00:00  2018-01-24 16:00:00  2018-01-24 17:00:00         ...           2018-01-24 14:00:00  2018-01-24 15:00:00  2018-01-24 16:00:00  2018-01-24 17:00:00  2018-01-24 18:00:00  2018-01-24 19:00:00  2018-01-24 20:00:00  2018-01-24 21:00:00  2018-01-24 22:00:00  2018-01-24 23:00:00\n",
      "symbol                          AIDBTC               AIDBTC               AIDBTC               AIDBTC               AIDBTC               AIDBTC               AIDBTC               AIDBTC               AIDETH               AIDETH         ...                        ZRXUSD               ZRXUSD               ZRXUSD               ZRXUSD               ZRXUSD               ZRXUSD               ZRXUSD               ZRXUSD               ZRXUSD               ZRXUSD\n",
      "open                            0.0002              7.9e-05              6.3e-05              7.2e-05                7e-05              6.1e-05              6.3e-05              7.3e-05              0.00055              0.00072         ...                           1.7                  1.7                  1.6                  1.6                  1.6                  1.6                  1.6                  1.7                  1.7                  1.6\n",
      "high                            0.0002               0.0001              7.5e-05              7.3e-05              7.7e-05              6.8e-05              7.3e-05              7.3e-05               0.0009              0.00072         ...                           1.7                  1.7                  1.6                  1.6                  1.6                  1.6                  1.7                  1.7                  1.7                  1.7\n",
      "low                              3e-05              4.7e-05                5e-05              6.1e-05              6.1e-05                6e-05              5.5e-05              6.5e-05              0.00051              0.00052         ...                           1.7                  1.6                  1.6                  1.6                  1.6                  1.6                  1.6                  1.7                  1.6                  1.6\n",
      "close                          0.00013              5.6e-05              7.2e-05                7e-05              6.1e-05              6.8e-05              7.3e-05              6.9e-05              0.00073               0.0006         ...                           1.7                  1.6                  1.6                  1.6                  1.6                  1.6                  1.7                  1.7                  1.7                  1.7\n",
      "volume                         2.8e+04              5.1e+04              1.1e+05              6.1e+04              2.6e+04              1.3e+03              3.1e+04              1.9e+04              5.4e+04              8.7e+04         ...                         3e+04              2.7e+04              5.9e+04              2.8e+04              1.2e+04              8.7e+03              4.6e+04              9.6e+04              2.2e+04              1.2e+03\n",
      "\n",
      "[7 rows x 2150 columns]\n"
     ]
    }
   ],
   "source": [
    "# 转置\n",
    "print(df.T)  # 将数据转置，行变成列，很有用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       AIDBTC\n",
      "1       AIDBTC\n",
      "2       AIDBTC\n",
      "3       AIDBTC\n",
      "4       AIDBTC\n",
      "         ...  \n",
      "2145    ZRXUSD\n",
      "2146    ZRXUSD\n",
      "2147    ZRXUSD\n",
      "2148    ZRXUSD\n",
      "2149    ZRXUSD\n",
      "Name: symbol, Length: 2150, dtype: object\n",
      "BTC\n",
      "**********\n",
      "0       AID\n",
      "1       AID\n",
      "2       AID\n",
      "3       AID\n",
      "4       AID\n",
      "       ... \n",
      "2145    ZRX\n",
      "2146    ZRX\n",
      "2147    ZRX\n",
      "2148    ZRX\n",
      "2149    ZRX\n",
      "Name: symbol, Length: 2150, dtype: object\n",
      "0       AIDBTC\n",
      "1       AIDBTC\n",
      "2       AIDBTC\n",
      "3       AIDBTC\n",
      "4       AIDBTC\n",
      "         ...  \n",
      "2145    ZRXUSD\n",
      "2146    ZRXUSD\n",
      "2147    ZRXUSD\n",
      "2148    ZRXUSD\n",
      "2149    ZRXUSD\n",
      "Name: symbol, Length: 2150, dtype: object\n",
      "0       aidbtc\n",
      "1       aidbtc\n",
      "2       aidbtc\n",
      "3       aidbtc\n",
      "4       aidbtc\n",
      "         ...  \n",
      "2145    zrxusd\n",
      "2146    zrxusd\n",
      "2147    zrxusd\n",
      "2148    zrxusd\n",
      "2149    zrxusd\n",
      "Name: symbol, Length: 2150, dtype: object\n",
      "0       6\n",
      "1       6\n",
      "2       6\n",
      "3       6\n",
      "4       6\n",
      "       ..\n",
      "2145    6\n",
      "2146    6\n",
      "2147    6\n",
      "2148    6\n",
      "2149    6\n",
      "Name: symbol, Length: 2150, dtype: int64\n",
      "**********\n",
      "0       AIDBTC\n",
      "1       AIDBTC\n",
      "2       AIDBTC\n",
      "3       AIDBTC\n",
      "4       AIDBTC\n",
      "         ...  \n",
      "2145    ZRXUSD\n",
      "2146    ZRXUSD\n",
      "2147    ZRXUSD\n",
      "2148    ZRXUSD\n",
      "2149    ZRXUSD\n",
      "Name: symbol, Length: 2150, dtype: object\n",
      "0        True\n",
      "1        True\n",
      "2        True\n",
      "3        True\n",
      "4        True\n",
      "        ...  \n",
      "2145    False\n",
      "2146    False\n",
      "2147    False\n",
      "2148    False\n",
      "2149    False\n",
      "Name: symbol, Length: 2150, dtype: bool\n",
      "0       AVTBTC\n",
      "1       AVTBTC\n",
      "2       AVTBTC\n",
      "3       AVTBTC\n",
      "4       AVTBTC\n",
      "         ...  \n",
      "2145    ZRXUSD\n",
      "2146    ZRXUSD\n",
      "2147    ZRXUSD\n",
      "2148    ZRXUSD\n",
      "2149    ZRXUSD\n",
      "Name: symbol, Length: 2150, dtype: object\n"
     ]
    }
   ],
   "source": [
    "# 字符串处理\n",
    "print(df['symbol'])\n",
    "print('BTCUSD'[:3])\n",
    "print('*' * 10)\n",
    "print(df['symbol'].str[:3])\n",
    "print(df['symbol'].str.upper())  # 加上str之后可以使用常见的字符串函数对整列进行操作\n",
    "print(df['symbol'].str.lower())\n",
    "print(df['symbol'].str.len())  # 计算字符串的长度,length\n",
    "print('*' * 10)\n",
    "df['symbol'].str.strip()  # strip操作，把字符串两边的空格去掉\n",
    "print(df['symbol'])\n",
    "print(df['symbol'].str.contains('AID'))  # 判断字符串中是否包含某些特定字符\n",
    "print(df['symbol'].str.replace('AID', 'AVT'))  # 进行替换，将sz替换成sh\n",
    "# 更多字符串函数请见：http://pandas.pydata.org/pandas-docs/stable/text.html#method-summary"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
